//
//  WaresDetailViewController.m
//  cityStar
//
//  Created by joyban on 14-12-26.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "WaresDetailViewController.h"
#import "StayViewController.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "UIColor+FlatUI.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

#import "discussCell.h"
#import "goodsDetailCell.h"
#import "ContentCell.h"
#import "AddressCell.h"
#import "shoppingDetailCell.h"
#import "RJPhoto.h"
#import "RJPhotoViewController.h"
#import "webView.h"
#import "NumberCell.h"
#import "CommodityDetail.h"
#import "RepairAndHomeViewController.h"

#import "ReviewViewController.h"
#import "INTULocationManager.h"
#import "MerchantDetail.h"

#import "ReadyPayViewController.h"
#import "BuycartGood.h"
#import "BuyCartViewController.h"

#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"
#import "HTDefine.h"

NSString *const numberCellID = @"NumberCellID";
NSString *const discussCellID = @"DiscussCellID";
NSString *const goodsTableViewCellID = @"goodsCellID";
NSString *const AddressCellID = @"AddressCellID";
NSString *const ContentCellID = @"contentCellID";
NSString *const ShoppingDetailCellID = @"shoppingDetailCellID";

@interface WaresDetailViewController ()<SGFocusImageFrameDelegate,CSbtnDelegate,CSAddressDelegate,CSgoodsDelegate,RJPhotoViewControllerDelegate,RJPhotoViewControllerDataSource>

@property (weak, nonatomic) IBOutlet UITableView *WaresTabel;
@property (weak,nonatomic) NSString *status;
@property (strong,nonatomic) SGFocusImageFrame *heardTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableDownLine;
@property (weak, nonatomic) IBOutlet UIButton *buybtn;
@property (strong, nonatomic) goodsDetailCell *GoodsetailCell;
@property (strong, nonatomic) NSString *commodityID;
@property (nonatomic, strong) CommodityDetail *commodity;
@property (nonatomic, strong) NumberCell *numberCell;
@property (weak, nonatomic) IBOutlet UIView *btnbackView;
@property (strong, nonatomic) NSMutableArray *payCommodityArray;
@property (strong, nonatomic) NSString *MoenyString;
@property (nonatomic, strong) NSString *popularity;
@property (nonatomic, strong) NSArray *collectArray;

@end

@implementation WaresDetailViewController

-(instancetype)initWithCommodityID:(NSString *)commodityID
{
    self = [super init];
    if (self) {
        self.commodityID = commodityID;
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"商品详情";
    self.navigationItem.title = self.navTitle;
    self.buybtn.layer.cornerRadius = 5;
    [self addBackButton];
    self.status = @"YES";
    [self setUpTableViews];
    [self webRequest_GetPopularity];
    [self setHeardTableViewWithImageUrls:nil];
    [self webRequest_getDetailMessageWithDetailID:self.commodityID];
    [self webRequest_getShoppingCollectMessage];
}


#pragma mark - webRequest

-(void)webRequest_GetPopularity
{
    NSDictionary *parameters = @{@"id":self.commodityID
                                 };
    [HttpRequestManager  postMethod:KinterFace_SupplierView parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
//            self.popularity = response[@"value"][0];
//            [SVProgressHUD showSuccessWithStatus:@"win"];
        }else{
//            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

-(void)webRequest_getDetailMessageWithDetailID:(NSString *)commotityID
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_GetCommodity,commotityID];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.commodity = [CommodityDetail objectFromDictionary:response[@"value"]];
            [self setHeardTableViewWithImageUrls:@[[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/640/h/300/m/1",self.commodity.image]]];
            [self selectWhichBtn];
            
            NSString *uuid = @"";
            if(self.commodity.collection != nil && ![self.commodity.collection isEqualToString:@""])
            {
                [self.GoodsetailCell.Collectionbtn setImage:[UIImage imageNamed:@"bcollection_y"] forState:UIControlStateNormal];
                self.status = @"NO";
                uuid = self.commodity.collection;
                
            }else{
                [self.GoodsetailCell.Collectionbtn setImage:[UIImage imageNamed:@"bcollection_n"] forState:UIControlStateNormal];
                self.status = @"YES";
                uuid = [[NSUUID UUID] UUIDString];
                
            }
            if([[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"]!=nil)
            {
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"UUID_Token"];
            }
            
            [[NSUserDefaults standardUserDefaults]setObject:uuid forKey:@"UUID_Token"];
            
            [self.WaresTabel reloadData];                                                                                                           
        }else{
            self.status = @"NO";
        }
    }];
}

-(void)webRequest_getShoppingCollectMessage
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_RecommentGoods,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]?KinterFace_RecommentSupplier,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]:@"122"];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.collectArray = [[NSArray alloc]initWithArray:response[@"value"]];
        }else
        {
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)selectWhichBtn
{
    self.sdkStatus = [NSString stringWithFormat:@"%@",self.commodity.on_sale];
    if([self.sdkStatus isEqualToString: @"1"])
    {
        self.tableDownLine.constant = 55;
        
    }else if ([self.sdkStatus isEqualToString:@"2"] )
    {
        self.tableDownLine.constant = 55;
        [self.buybtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [self.buybtn setTitle:@"加入购物车" forState:UIControlStateHighlighted];
        [self.buybtn setBackgroundImage:[UIImage imageNamed:@"orangeButton"] forState:UIControlStateNormal];
        [self.buybtn setBackgroundImage:[UIImage imageNamed:@"orangeButton_touch"] forState:UIControlStateHighlighted];

    }else if([self.sdkStatus isEqualToString:@"0"]){
        [self.btnbackView removeFromSuperview];
        self.tableDownLine.constant = -10;

    }else{
        
    }
}

//返回方法重写
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - collectionCell btndelegate

-(IBAction)butBtn_Action:(UIButton *)sender
{
    self.sdkStatus = [NSString stringWithFormat:@"%@",self.commodity.on_sale];

    if([self.sdkStatus isEqualToString:@"2"])
    {
        NSString *uuid = [[NSUUID UUID] UUIDString];
        NSDictionary *parameters = @{@"uuid":uuid,
                                     @"id":self.commodityID,
                                     @"number":self.numberCell.numberTextField.text};
        [HttpRequestManager postMethod:KInterface_CartAdd parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            DLog(@"%@",response);
            if (success) {
                RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"查看购物车" action:^{
                    BuyCartViewController *BuyCartVC = [[BuyCartViewController alloc]init];
                    [self.navigationController pushViewController:BuyCartVC animated:YES];
                }];
                RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"继续购物"];
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"添加成功,是否进入购物车进行结算？" message:@"" cancelButtonItem:rightButton otherButtonItems:leftButton, nil];
                [alertView show];
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];

    }else{
        [HttpRequestManager getMethod:KInterFace_getPersonalDetail parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if(success)
            {
                [self getselectCommodity];
                ReadyPayViewController *readyPayCommodityVC = [[ReadyPayViewController alloc]initWithCommodityArray:self.payCommodityArray andMoney:self.MoenyString];
                [self.navigationController pushViewController:readyPayCommodityVC animated:YES];
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];
    }
}

-(void)getselectCommodity
{
    self.payCommodityArray = [[NSMutableArray alloc]init];
    BuycartGood *goodcart = [[BuycartGood alloc]init];
    goodcart.create_time = [NSString stringWithFormat:@"%@",self.commodity.create_time];
    goodcart.goods_id = self.commodityID;
    goodcart.goods_name = self.commodity.name;
    goodcart.images = [NSString stringWithFormat:@"%@",self.commodity.image];
    goodcart.market_price = self.commodity.market_price;
    goodcart.number = self.numberCell.numberTextField.text;
    goodcart.price = [NSString stringWithFormat:@"%@",self.commodity.special_price];
    [self.payCommodityArray addObject:goodcart];
    
    self.MoenyString = [NSString stringWithFormat:@"%i",[goodcart.price integerValue]*[goodcart.number integerValue]];
}
//4张图片
-(void)senderBtnClick:(UIButton *)btn
{
    WaresDetailViewController *wareViewController = [[WaresDetailViewController alloc]initWithCommodityID:[NSString stringWithFormat:@"%i",btn.tag]];
    [self.navigationController pushViewController:wareViewController animated:true];
}

//收藏
-(void)sendergoodsClick:(UIButton *)btn
{
    if([self.status isEqualToString:@"YES"])
    {
        NSString *uuid = [[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"];
        if(uuid == nil)
        {
            uuid = [[NSUUID UUID] UUIDString];
            [[NSUserDefaults standardUserDefaults]setObject:uuid forKey:@"UUID_Token"];
        }
        NSDictionary *parameters = @{@"uuid":uuid,
                                     @"id":self.commodity.objectId,
                                     @"type":@"0"};
        
        [HttpRequestManager postMethod:KInterface_Collection parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            DLog(@"%@",response);
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
                [self.GoodsetailCell.Collectionbtn setImage:[UIImage imageNamed:@"bcollection_y"] forState:UIControlStateNormal];
                self.status = @"NO";
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }            }
        }];

    }else{
        NSString *uuid = [[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"];
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%@",KInterface_RemoveCollection,uuid,@"0"];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                NSLog(@"%@",response);
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"UUID_Token"];
                
                [SVProgressHUD showSuccessWithStatus:@"取消收藏"];
                [self.GoodsetailCell.Collectionbtn setImage:[UIImage imageNamed:@"bcollection_n"] forState:UIControlStateNormal];
                self.status = @"YES";
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];
    }
}

-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

//打电话
-(void)senderContactClick:(NSString *)telNum
{
    RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"确定" action:^{
        NSString *telStr=[@"tel://" stringByAppendingString:telNum];
        NSURL *url=[NSURL URLWithString:telStr];
        [[UIApplication sharedApplication]openURL:url];
    }];
    RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"取消"];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否拨打该号码？" message:telNum cancelButtonItem:rightButton otherButtonItems:leftButton, nil];
    [alertView show];
}
//进入商铺
-(void)senderintoactClick:(NSString *)merchantID
{
    RepairAndHomeViewController *repairAndHomeVC = [[RepairAndHomeViewController alloc]initWithMerchantID:merchantID];
    [self.navigationController pushViewController:repairAndHomeVC animated:YES];
}

//- (IBAction)buyBtn_Action:(UIButton *)sender {
//    if(self.sdkStatus == SdkStatusShoping)
//    {//立即购买方法
//        
//        
//    }else if (self.sdkStatus == SdkStatusShopingTwo)
//    {//加入购物车方法
//        
//    }
//
//}

//-(IBAction)btnPhoto_Touch:(id)sender
//{
//    RJPhotoViewController *photoVC = [[RJPhotoViewController alloc] initWithDataSource:self delegate:self];
//    photoVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:photoVC animated:YES];
//}

#pragma mark - RJPhotoView DataSource
- (NSInteger)photoViewNumberOfPhoto
{
//    return [self.currentMoment.imgList count];
    return 5;
}

- (RJPhoto *)photoView:(RJPhotoViewController *)photoView photoForRow:(NSUInteger)row
{
    RJPhoto *photo = [[RJPhoto alloc] init];
//    MomentImg *momentImg = self.currentMoment.imgList[row];
    photo.URL = [NSURL URLWithString:@"http://classimage.b0.upaiyun.com/201412/jre5hnts.png"];
    photo.title = @"hello world";
    photo.caption = @"zxznzxncjkjncskjdcnskjdnckjdsnkjccszx";
    return photo;
}

#pragma mark - RJPhotoView Delegate
- (NSInteger)numberOfCommentCount
{
//    return [self.currentMoment.commCount integerValue];
    return 5;
}

- (void)photoViewDidSelectCommentButton
{
//    [self _goToCommentListVCWithMoment:self.currentMoment];
}

- (void)_goToCommentListVCWithMoment:(Moment *)moment
{
//    CommentListVC *commentListVC = [[CommentListVC alloc] initWithMoment:moment];
//    [self.navigationController pushViewController:commentListVC animated:YES];
}

#pragma mark - 轮转
-(void)setHeardTableViewWithImageUrls:(NSArray *)urls
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSInteger length;
    if (urls) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:urls[0],@"imagePath",nil];
        length = tempArray.count+1;
        [tempArray addObject:dict];

    }else{
        tempArray = [NSMutableArray array];
        length = 1;
        for (int i = 0 ; i < length; i++)
        {
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"banner",@"imagePath",nil];
            [tempArray addObject:dict];
        }
    }
    //添加最后一张图 用于循环
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2];
    if (length > 1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:length-1];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:-1] ;
        [itemArray addObject:item];
    }
    for (int i = 0; i < length; i++)
    {
        NSDictionary *dict = [tempArray objectAtIndex:i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:i];
        [itemArray addObject:item];
    }
    //添加第一张图 用于循环
    if (length >1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:0];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:length];
        [itemArray addObject:item];
    }
    self.heardTableView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) delegate:self imageItems:itemArray isAuto:NO];
    [self.heardTableView scrollToIndex:2];
    UILabel *labelPopularity = [[UILabel alloc]init];
    labelPopularity.text = [NSString stringWithFormat:@"%@+人气",self.commodity.views];
    labelPopularity.userInteractionEnabled = YES;
    labelPopularity.backgroundColor = [UIColor blackColor];
    labelPopularity.textColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    labelPopularity.font = [UIFont systemFontOfSize:10];
    labelPopularity.layer.cornerRadius = 5.0f;
    labelPopularity.textAlignment = NSTextAlignmentCenter;
    labelPopularity.clipsToBounds = YES;
    labelPopularity.frame = CGRectMake(AppBounds.size.width-85, 20, 70, 20);
    [self.heardTableView addSubview:labelPopularity];
    self.WaresTabel.tableHeaderView = self.heardTableView;
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item ImagePage:(int)pageIndex
{
    
}

-(void)setUpTableViews
{
    [self.WaresTabel registerNib:[NumberCell nib] forCellReuseIdentifier:numberCellID];
    [self.WaresTabel registerNib:[discussCell nib] forCellReuseIdentifier:discussCellID];
    [self.WaresTabel registerNib:[goodsDetailCell nib] forCellReuseIdentifier:goodsTableViewCellID];
    [self.WaresTabel registerNib:[ContentCell nib] forCellReuseIdentifier:ContentCellID];
    [self.WaresTabel registerNib:[shoppingDetailCell nib] forCellReuseIdentifier:ShoppingDetailCellID];
    [self.WaresTabel registerNib:[AddressCell nib] forCellReuseIdentifier:AddressCellID];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            return [goodsDetailCell heightOfTableCell];
        }else if (indexPath.row == 1)
        {
            if ([self.commodity.on_sale integerValue] == 3) {
                return [NumberCell heightOfTableCell] + 10;
            }else{
                return [NumberCell heightOfTableCell] - 40;
            }
        }else if(indexPath.row == 2)
        {
            return [discussCell heightOfTableCell];
        }else
        {
            if([self.commodity.brief isEqualToString:@""] || self.commodity.brief == nil)
            {
                return [ContentCell heightOfTableCell]-40;
            }else{
                return [ContentCell heightOfTableCell];
            }
        }
    }else if(indexPath.section == 1){
        return [AddressCell heightOfTableCell];
    }else{
        return [shoppingDetailCell heightOfTableCell];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 2)
    {
        return 30;
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 2)
    {
        return 0;
    }else{
        return 10;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 4;
    }else{
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F6F5F1"]];
    return sectionView;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *imageOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
    [imageOne setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
    
    UIImageView *imageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 29, tableView.bounds.size.width, 1)];
    [imageTwo setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
    
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(15, 5, 200, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"EEEEEE"]];
    [sectionView addSubview:label];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 20, 20)];
    [imageView setImage:[UIImage imageNamed:@"recom"]];
    [sectionView addSubview:imageView];
    [sectionView addSubview:imageOne];
    [sectionView addSubview:imageTwo];
    
    [label setFrame:CGRectMake(45, 5, 200, 20)];
    label.textColor = [UIColor colorFromHexCode:@"8E8E8E"];
    label.text = @"到过这里的人还看过";
    
    return sectionView;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            self.GoodsetailCell = [tableView dequeueReusableCellWithIdentifier:goodsTableViewCellID];
            self.GoodsetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
            self.GoodsetailCell.delegate = self;
            self.GoodsetailCell.NameLabel.text = [NSString stringWithFormat:@"%@",self.commodity.name];
            self.GoodsetailCell.NewPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.commodity.special_price];
//           self.GoodsetailCell.OldPriceLabel.text = self.commodity.market_price;
//            self.OldPriceLabel
            NSString *oldPrice = [NSString stringWithFormat:@"￥%@",self.commodity.market_price];
            NSUInteger length = [oldPrice length];
            NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
            [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
            [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, length)];
            [self.GoodsetailCell.OldPriceLabel setAttributedText:attri];
            
            return self.GoodsetailCell;
        }else if(indexPath.row == 3){
            ContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:ContentCellID];
            contentCell.selectionStyle = UITableViewCellSelectionStyleGray;
            if (contentCell==nil) {
                contentCell=[[ContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ContentCellID];
            }
            contentCell.contentLabel.text = self.commodity.brief;
            //contentCell.contentLabel.text = @"11231233123";
            if(contentCell.contentLabel.text.length > 46)
            {
                if (self.commodity.brief.length > 6) {
                    NSString *btntext = [NSString stringWithFormat:@"%@..",[self.commodity.brief substringWithRange:NSMakeRange(0, 46)]];
                    contentCell.contentLabel.text = btntext;
                }
            }
            return contentCell;
            
        }else if(indexPath.row == 2){
            //TODO: 评论数没有参数
            discussCell *DiscussCell = [tableView dequeueReusableCellWithIdentifier:discussCellID];
            DiscussCell.discussLabel.text = [NSString stringWithFormat:@"(%@)",self.commodity.comment];
//            [self.commodity.comment intValue]
            DiscussCell.selectionStyle = UITableViewCellSelectionStyleGray;
            return DiscussCell;
        }else
        {
            self.numberCell = [tableView dequeueReusableCellWithIdentifier:numberCellID];
            self.numberCell.selectionStyle = UITableViewCellSelectionStyleNone;
            self.numberCell.numberTextField.text = @"1";
            if ([self.commodity.on_sale intValue] == 3) {
                self.numberCell.dateBeginPicker.hidden = NO;
                self.numberCell.dateEndPicker.hidden = NO;
                self.numberCell.lineImage.hidden = NO;
                self.numberCell.timeImage.hidden = NO;
            }else{
                self.numberCell.dateBeginPicker.hidden = YES;
                self.numberCell.dateEndPicker.hidden = YES;
                self.numberCell.lineImage.hidden = YES;
                self.numberCell.timeImage.hidden = YES;
            }
            return self.numberCell;
        }
        
    }else if(indexPath.section == 1){
        AddressCell *addressCell = [tableView dequeueReusableCellWithIdentifier:AddressCellID];
        addressCell.selectionStyle = UITableViewCellSelectionStyleGray;
        addressCell.delegate = self;
        [addressCell configWithAddressID:self.commodity.supplier_id];
        
        //TODO:商家电话和地址参数没有和商家名称
//        addressCell.labelTelNum.text = self.commodity.
        return addressCell;
    }else{
        shoppingDetailCell *ShoppingDetailCell = [tableView dequeueReusableCellWithIdentifier:ShoppingDetailCellID];
        [ShoppingDetailCell configWithShoppingMessage:self.collectArray];
        ShoppingDetailCell.delegate = self;
        ShoppingDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return ShoppingDetailCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        if(indexPath.row == 3)
        {
            NSString *btntext = @"";
            if(self.commodity.name.length > 6)
            {
                btntext = [NSString stringWithFormat:@"%@...",[self.commodity.name substringWithRange:NSMakeRange(0, 6)]];
            }else{
                btntext = [NSString stringWithFormat:@"%@",self.commodity.name];
            }
            
            if([self.commodity.name isEqualToString:@""] || self.commodity.name == nil)
            {
                btntext = @"商品详情";
            }
            webView *webViewController = [[webView alloc]init];
            webViewController.navTitle = btntext;
            webViewController.goodsid = self.commodityID;
            webViewController.type = @"goods";
            [self.navigationController pushViewController:webViewController animated:YES];
        }
        
        if(indexPath.row == 2)
        {
            ReviewViewController *ReviewController = [[ReviewViewController alloc]initWithCommodityID:self.commodityID];
            [self.navigationController pushViewController:ReviewController animated:YES];
        }

    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            RepairAndHomeViewController *repairAndHomeVC = [[RepairAndHomeViewController alloc]initWithMerchantID:self.commodity.supplier_id];
            [self.navigationController pushViewController:repairAndHomeVC animated:YES];
        }
    }
    [tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
